package com.jeek.jdbc.myCode.homeWork.preparestatement;

import com.jeek.jdbc.myCode.homeWork.entity.Admin;
import com.jeek.jdbc.myCode.homeWork.utils.JDBCUtils;

import java.sql.*;

public class AdminDAO {
    // 从数据库查询管理员（按用户名和密码匹配）
    public Admin queryAdmin(String username, String password) throws SQLException {
        Connection conn = null;
        PreparedStatement ppst = null;
        ResultSet rs = null;
        Admin admin = null;

        try {
            conn = JDBCUtils.getConn();
            String sql = "SELECT * FROM admin WHERE username = ? AND password = ?";
            ppst = conn.prepareStatement(sql);
            ppst.setString(1, username);
            ppst.setString(2, password);
            rs = ppst.executeQuery();

            if (rs.next()) {
                admin = new Admin();
                admin.setAdminId(rs.getInt("admin_id"));
                admin.setUsername(rs.getString("username"));
                admin.setPassword(rs.getString("password"));
                admin.setCreateTime(rs.getTimestamp("create_time"));
            }
        } finally {
            JDBCUtils.close(ppst, conn, rs);
        }
        return admin;
    }
}